import json

from src.video.baseInfo.VideoFolderInfo import VideoFolderInfo
from src.video.baseInfo.VideoHeapsInfo import VideoHeapsInfo,HeapInfo
from src.utils import  file_utils,matrix_utils,ffmpeg_utils
from src.algo.image_smilarity.similarity import Similarity
import os


class VideoSimilaryCaculator(object):
    '''
    视频图片相似度计算相关类
    '''

    def __init__(self, video_folder_info: VideoFolderInfo, pic_num_per_second: int):
        '''

        :param video_folder_info:
        :param pic_num_per_second: 每一秒抽帧数量
        '''
        self.video_folder_info = video_folder_info
        self.pic_num_per_second = pic_num_per_second
        self.init = False


    def get_similary_model(self) -> Similarity:
        if not self.init:
            self.similary_model = Similarity()
            self.init = True
        return self.similary_model

    def _get_video_heaps(self, image_path, test=False,grap_second=60,top_num=6) -> VideoHeapsInfo:
        '''
        加载image_path里面图片，向量化以后，根据相似度分割成不同的段落
        :param image_path:
        :return:
        '''
        image_urls = file_utils.load_files(image_path)
        if test:
            image_urls = image_urls[:200]
        image_features = self.get_similary_model().get_image_feature(image_urls)
        videoHeapsInfo = VideoHeapsInfo(image_features,test=test,grap_sencond=grap_second,top_num=top_num)
        return videoHeapsInfo





    def ffmpeg_chouzhen(self):
        '''
        生成相应的视频抽帧图片
        :return:
        '''
        if len(os.listdir(self.video_folder_info.origin_image_fold)) <= 10:
            ffmpeg_utils.grab_images_from_video(self.video_folder_info.origin_cut_mp4_file, self.video_folder_info.origin_image_fold, self.pic_num_per_second +1)

        if len(os.listdir(self.video_folder_info.jieshuo_image_fold)) <= 10:
            ffmpeg_utils.grab_images_from_video(self.video_folder_info.jieshuo_mp4_file, self.video_folder_info.jieshuo_image_fold, self.pic_num_per_second)







    def process(self,test=False,grap_second=60,top_num=6) -> list:
        # 第一步，抽帧
        self.ffmpeg_chouzhen()

        # 第二步，生成相似下标
        jieshuoHeapsLists = []
        if file_utils.check_file_exists(self.video_folder_info.jieshuoHeapConvertFile):
            with open(self.video_folder_info.jieshuoHeapConvertFile, 'r', encoding='utf-8') as fread:
                videoHeapStr = json.load(fread)
                for item in videoHeapStr:
                    heapInfo = HeapInfo()
                    heapInfo.init_from_dict(json.loads(item))
                    jieshuoHeapsLists.append(heapInfo)

            temp = VideoHeapsInfo(grap_sencond=grap_second,top_num=top_num)
            temp.heapInfoList = jieshuoHeapsLists
            temp.merge_adjacent_node()
            jieshuoHeapsLists = temp.heapInfoList

        else:
            jieshuoHeapsInfo = self._get_video_heaps(self.video_folder_info.jieshuo_image_fold,test=test,grap_second=grap_second,top_num=top_num)
            originHeapsInfo = self._get_video_heaps(self.video_folder_info.origin_image_fold,test=test,grap_second=grap_second,top_num=top_num)

            jieshuoHeapsInfo.update_heap_relative_heaps(originHeapsInfo)

            jieshuoHeapsInfo.save_to_file(self.video_folder_info.jieshuoHeapConvertFile)
            jieshuoHeapsInfo.merge_adjacent_node()
            jieshuoHeapsLists = jieshuoHeapsInfo.heapInfoList
        return jieshuoHeapsLists




            
